package com.wxh.security;

import com.wxh.pojo.Permission;
import com.wxh.pojo.Role;
import com.wxh.pojo.User;
import com.wxh.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
public class SpringSecurityUserService implements UserDetailsService {
    @Reference
    private UserService userService;

    public UserDetails loadUserByUsername(String username) {
        User user = userService.findByUsername(username);
        if (user == null) {
            return null;
        }
        List<GrantedAuthority> list=new ArrayList<>();
        Set<Role> roles=user.getRoles();
        if(roles!=null && roles.size()>0) {
            for (Role role : roles) {
                String keyword = role.getKeyword();
                list.add(new SimpleGrantedAuthority(keyword));
                for (Permission permission : role.getPermissions()) {
                    String permissionKeyword = permission.getKeyword();
                    list.add(new SimpleGrantedAuthority(permissionKeyword));
                }
            }
        }
        return new org.springframework.security.core.userdetails.User(username,user.getPassword(),list);
    }
}
